package hot100;

import java.util.*;

public class groupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        //hash表   key：strs[n].tocharArray.sort   value:List<String>  strs[n]
        Map<String,List<String>> hash = new HashMap<>();

        //遍历strs
        for(String s : strs ) {
            char[] tem = s.toCharArray();
            Arrays.sort(tem);
            //得出key
            String key = new String(tem);
            //获取对应key的value值
            List<String> list = hash.getOrDefault(key,new ArrayList<>());
            //添加新的字母异位词
            list.add(s);
            //重新放回哈希表中
            hash.put(key,list);
        }
        return new ArrayList<List<String>>(hash.values());
    }
}
